#delimit;
clear;
set more off;

global temp /Sastemp;
global path ~;
set mem 5000m;

/*================================================
 Program: regs_cd.do
 Author:  Avi Ebenstein
 Created: August 2007
 Purpose: OLS regressions of death rates by cause on water quality
=================================================*/

use ~/pollution/datafiles/dsp_basins.dta, replace;
 
gen digestivecancer_o=deathrate9;
gen noncancer_o=deathrate_o-cancer_o;
gen lungcancer_o=deathrate10;
gen othercancer_o=cancer_o-deathrate9-deathrate10;

gen digestivecancer_m=maledr9;
gen noncancer_m=deathrate_m-cancer_m;
gen lungcancer_m=maledr10;
gen othercancer_m=cancer_m-maledr9-maledr10;

gen digestivecancer_f=femaledr9;
gen noncancer_f=deathrate_f-cancer_f;
gen lungcancer_f=femaledr10;
gen othercancer_f=cancer_f-femaledr9-femaledr10;

global causelist "
deathrate_m
cancer_m
digestivecancer_m
lungcancer_m
othercancer_m
noncancer_m
heart_m
stroke_m
respiratory_m
violent_m
other_m
";

****************************;
* List of control variables ;
****************************;

global controls "yrsed farmer urban lnairpollution i.region i.cla3";

****************************;
* List of chemicals         ;
****************************;

global mylist "overall_q a_n bod dissolved_ lead mercury oils permanga_n a_n volatile_p";
global mylist "overall_q";

local causes "deathrate cancer digestivecancer lungcancer othercancer noncancer heart other respiratory stroke violent";

gen mypop_o=totalpop;
gen mypop_m=malepop;
gen mypop_f=femalepop;

gen str1 group="";;
global sexlist "o m f";

foreach k of global sexlist{;
replace group="`k'";                            
foreach j of local causes{;
                  gen lndeathrate_`j'_`k'=ln(`j'_`k');
foreach i of global mylist{;
                           preserve;
                           replace `i'=. if `i'==0;
                           xi: reg lndeathrate_`j'_`k' `i' $controls [w=mypop_`k'],cluster(provgb) robust;
                           gen beta=_b[`i'];
                           gen se=_se[`i'];
                           gen t=beta/se;
                           gen str cause="`j'";
                           gen str chemical="`i'";
                           keep cause chemical beta se t group;
                           keep in 1;
                           save ~/pollution/datafiles/run_`i'_`j'_`k', replace;
                           restore;
                         };
               };
                          };
clear;
set obs 1;
                 gen a=1;
foreach k of global sexlist{;                 
foreach j of local causes{;
foreach i of global mylist{;
                           append using ~/pollution/datafiles/run_`i'_`j'_`k';
                         };
               };
};

replace cause="a_deathrate" if cause=="deathrate";
replace cause="b_cancer" if cause=="cancer";
replace cause="c_digestivecancer" if cause=="digestivecancer";
replace cause="d_lungcancer" if cause=="lungcancer";
replace cause="e_othercancer" if cause=="othercancer";
replace cause="f_noncancer" if cause=="noncancer";
replace cause="g_heart" if cause=="heart";
replace cause="h_stroke" if cause=="stroke";
replace cause="i_respir" if cause=="respiratory";
replace cause="j_violent" if cause=="violent";
replace cause="k_other" if cause=="other";

replace chemical="a_a_overall_q" if chemical=="overall_q";
                 
table cause group, c(mean beta mean se) f(%9.3fc) cellwidth(6);                 

!rm ~/pollution/datafiles/run_*.dta;
use ~/pollution/datafiles/dsp_basins.dta, replace;
